package com.hk.webapp.controller.client;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hk.webapp.annotations.AllowAnonymous;
import com.hk.webapp.bean.CareerCity;
import com.hk.webapp.bean.CareerInfo;
import com.hk.webapp.bean.CareerJob;
import com.hk.webapp.bean.CareerType;
import com.hk.webapp.bo.CareerInfoBo;
import com.hk.webapp.service.CareerCityService;
import com.hk.webapp.service.CareerInfoService;
import com.hk.webapp.service.CareerJobService;
import com.hk.webapp.service.CareerTypeService;
import com.hk.webapp.vo.GetCareerListVo;
import com.hk.webapp.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController("ClientCareerController")
public class CareerController {
    @Resource
    private CareerCityService careerCityService;
    @Resource
    private CareerTypeService careerTypeService;
    @Resource
    private CareerJobService careerJobService;
    @Resource
    private CareerInfoService careerInfoService;

    @PostMapping("/Career/GetCareerEnum")
    @AllowAnonymous
    public ResultVo GetCareerEnum(){
        List<CareerCity> career_city_list = careerCityService.GetEntityList();
        List<CareerJob> career_job_list = careerJobService.GetEntityList();
        List<CareerType> career_type_list = careerTypeService.GetEntityList();

        HashMap<String,Object> map= Maps.newHashMap();
        map.put("cityList",career_city_list.parallelStream().map(c->{
            HashMap<String,Object> _map=Maps.newHashMap();
            _map.put("id",c.getId());
            _map.put("name",c.getName());
            return _map;
        }).collect(Collectors.toList()));
        map.put("career_job_list",career_job_list.parallelStream().map(c->{
            HashMap<String,Object> _map=Maps.newHashMap();
            _map.put("id",c.getId());
            _map.put("name",c.getName());
            return _map;
        }).collect(Collectors.toList()));
        map.put("career_type_list",career_type_list.parallelStream().map(c->{
            HashMap<String,Object> _map=Maps.newHashMap();
            _map.put("id",c.getId());
            _map.put("name",c.getName());
            return _map;
        }).collect(Collectors.toList()));

        return ResultVo.createBySuccessData(map);
    }

    @PostMapping("/Career/GetList")
    @AllowAnonymous
    public ResultVo GetCareerList(@RequestBody GetCareerListVo vo){
        PageInfo<CareerInfoBo> list = careerInfoService.GetCareerList(vo.getPageIndex(),vo.getPageSize(),vo.getCity_id(),vo.getJob_id(),vo.getType_id(),1);
        return ResultVo.createBySuccessData(list.getList(),list.getTotal());
    }
    @PostMapping("/Career/Detail")
    @AllowAnonymous
    public ResultVo Detail(Integer id){
        if (id!=null&&id>0)
        {
            CareerInfo info = careerInfoService.GetEntityById(id);
            if (info==null)
            {
                return ResultVo.createByError("数据不存在或已删除");
            }
            CareerCity city_info = careerCityService.GetEntityById(info.getCityId());
            CareerJob job_info = careerJobService.GetEntityById(info.getJobId());
            CareerType type_info = careerTypeService.GetEntityById(info.getTypeId());

            HashMap<String,Object> map=Maps.newHashMap();
            map.put("id",info.getId());
            map.put("company",info.getCompany());
            map.put("duties_content",info.getDutiesContent());
            map.put("qualified_content",info.getQualifiedContent());
            map.put("title",info.getTitle());
            map.put("city_name",city_info.getName());
            map.put("job_name",job_info.getName());
            map.put("type_name",type_info.getName());
            return ResultVo.createBySuccessData(map);
        }
        return ResultVo.createByError("数据不存在或已删除");
    }
}
